﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;

using Htss.Platform.Data;
using DreamBox;
namespace DreamBox.DAL.User
{
    public class UserInfo
    {
        /// <summary>
        /// 验证邮箱能否注册
        /// </summary>
        /// <param name="Email"></param>
        /// <returns></returns>
        public bool IsUserName(string UserName)
        {
            DbAction action = new DbAction();
            action.SetFields("UserName",UserName);
            return action.Select("select UserID from dbox_User where UserName=@UserName", true).Rows.Count < 1;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public GHashTable Delete(int UserID)
        {
            DbProc gProc = new DbProc("dbox_proc_DeleteUser");
            gProc.SetFields("@UserID", UserID);
            gProc.SetFields("@ReturnValue", 0, SqlDbType.Int, 4, ParameterDirection.ReturnValue);
            return gProc.ExecNonQueryHashTable();
        }
        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <returns></returns>
        public bool UpdateAvatar(int UserID, string Avatar)
        {
            DbAction action = new DbAction("dbox_User");
            action.SetFields("UserID", UserID, DbFieldType.Condition);
            action.SetFields("Avatar", Avatar);
            return action.Update();
        }
        /// <summary>
        /// 更新用户状态
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="ban"></param>
        /// <returns></returns>
        public int UpdateLockBan(int UserID,int IsStatus,int PostUserID)
        {
            DbProc gProc = new DbProc("dbox_proc_LockUser");
            gProc.SetFields("@UserID",UserID);
            gProc.SetFields("@IsStatus", IsStatus);
            gProc.SetFields("@PostUserID", PostUserID);
            gProc.SetFields("@returnValue", 0, SqlDbType.Int, 4, ParameterDirection.ReturnValue);
            return int.Parse(gProc.ExecNonQueryHashTable()["@returnValue"].ToString());
        }
        /// <summary>
        /// 禁用/取消禁用
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="IsStatus"></param>
        /// <returns></returns>
        public bool UpdateStatus(int UserID,bool IsStatus)
        {
            DbAction action = new DbAction("dbox_User");
            action.SetFields("UserID", UserID, DbFieldType.Condition);
            action.SetFields("IsStatus",IsStatus);
            return action.Update();
        }
        /// <summary>
        /// 编辑密码
        /// </summary>
        /// <returns></returns>
        public bool UpdatePassword(int UserID, string Password)
        {
            DbAction action = new DbAction("dbox_User");
            action.SetFields("UserID", UserID,DbFieldType.Condition);
            action.SetFields("Password", Password);
            return action.Update();
        }
        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <returns></returns>
        public bool Update(DreamBox.Model.User m)
        {
            DbAction action = new DbAction("dbox_User");
            action.SetFields("UserID", m.UserID,DbFieldType.Condition);
            action.SetFields("UserName", m.UserName);
            action.SetFields("Sex",m.Sex);
            action.SetFields("Tags", m.Tags != null && m.Tags != "" ? m.Tags : "");
            action.SetFields("Signature", m.Signature != null && m.Signature != "" ? m.Signature : "");
            action.SetFields("Location", m.Location != null && m.Location != "" ? m.Location : "");
            return action.Update();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="m">实体对象</param>
        /// <returns></returns>
        public GHashTable Add(DreamBox.Model.User m)
        {
            DbProc gProc = new DbProc("dbox_proc_AddUser");
            gProc.SetFields("@UserName",m.UserName);
            gProc.SetFields("@Password", FormsAuthentication.HashPasswordForStoringInConfigFile(m.Password, "MD5"));
            gProc.SetFields("@Email",m.Email);
            gProc.SetFields("@ReturnValue", 0, SqlDbType.Int, 4, ParameterDirection.ReturnValue);
            return gProc.ExecNonQueryHashTable();
        }
    }
}
